package com.cuz.hot100;

import com.cuz.daileetcode.utils.ListNode;

public class Q61rotateRight {


    public static ListNode solution2(ListNode head, int k) {
        if (head == null || head.next == null || k <= 0) {
            return head;
        }
        ListNode tail = head;
        int len = 1;
        while (tail.next != null) {
            tail = tail.next;
            len++;
        }
        k = k % len;
        if (k == 0) {
            return head;
        }
        ListNode pre = new ListNode(0);
        pre.next = head;

        ListNode cur = head;
        for (int i = 0; i < len - k; i++) {
            cur = cur.next;
            pre = pre.next;
        }
        tail.next = head;
        pre.next = null;
        return cur;
    }
}
